<?php
include_once "./conf.php";
include_once "./functions.php";
include_once "./class.TemplatePower.inc.php";
include_once "./rrdwrapper.php";
include_once "./get_context.php";

$tpl = new TemplatePower( template("mrjob_view.tpl") );
$tpl->prepare();

//Generate myjob selection list
$jobsdir = $rrds."/mrjob";
$joblist = getDirEntryList($jobsdir);
sort($joblist);
$jobid = $optvals['jobid'];
if ($jobid == NULL) {
  if (count($joblist) > 0) {
    $jobid = $joblist[0];
  } else {
    $jobid = "";
  }
}
$optvals['jobid']=$jobid;
genSelectList($tpl, "mrjoblist", $joblist, $jobid, NULL);

//Generate metric selection list
$metric = $optvals['metric'];
if ($metric == NULL) {
  $metric = $hadoopMetricList[0];
}
genSelectList($tpl, "metriclist", $hadoopMetricList, $metric, $metric_display);

//Generate time span
$tpl->newBlock("timerange");
if (array_key_exists("start", $optvals)) {
  $cs = $optvals["start"];
  $tpl->assign("cs_value", "VALUE=\"$cs\"");
}
if (array_key_exists("end", $optvals)) {
  $ce = $optvals["end"];
  $tpl->assign("ce_value", "VALUE=\"$ce\"");
}

if ($jobid != "") {
  // Generate job-cluster overview
  $optvals['metric'] = NULL;
  $args=genArgList($string_opts, $optvals);
  $args.=genArgList($number_opts, $optvals);
  $cnt = 0;
  foreach ($hadoopMetricList as $item) {
    $cnt += 1;
    $tpl->newBlock("overview");
    $imgargs = "t=jobcluster&amp;m=$item&amp;$args";
    $host_link="./graph.php?$imgargs&amp;z=large";
    $tpl->assign("imgargs", $imgargs);
    $tpl->assign("hostlink", $host_link);
    $tpl->assign("overview_tip", $metric_tips[$item]);
    if ($cnt % 3 == 0)
      $tpl->assign("br", "</TR><TR><TD><br><br></TD></TR><TR>");
  }

  if (array_key_exists($metric, $metric_display)) {
    $tpl->newBlock("nodeview_title");
    $tpl->assign("nodeview_metric","With $metric_display[$metric]");
  }

  // Generate job-node overview
  $myjobdir = $jobsdir."/$jobid";
  $hostlist = getDirEntryList($myjobdir);
  sort($hostlist);
  $nnode = 0;
  $args1 = $args."&amp;t=jobnode&amp;m=$metric";
  foreach ($hostlist as $node) {
    $tpl->newBlock("nodeview");
    $nodeargs = $args1."&amp;h=$node";
    $host_link = "./graph.php?$nodeargs&amp;z=large";
    $cell="<td><a href=$host_link>"."<img src=\"./graph.php?$nodeargs&amp;z=$size\" "." border=0></a></td>";
    if ($nnode % 3 == 2)
      $tpl->assign("br", "</tr><tr>");
    $tpl->assign("metric_image", $cell);
    $nnode += 1;
  }
}

$tpl->printToScreen();
?>

